import React from 'react';
import {parsePercent, numberHandle, safeMultiple} from '~/utils/number';
import CommonEchart from '../../../common/CommonEchart';

const tooltipCallback = (params = []) => {
    const tooltipList = params.map((chartItem, index) => {
        const {marker, name, seriesName, value} = chartItem;
        let formatValue = v => v;
        let result = '';
        if (seriesName === '直播推题总数') {
            formatValue = value => numberHandle(value, 2) || formatValue;
        } else {
            formatValue = parsePercent || formatValue;
        }
        if (index === 0) {
            result = `${name}<br/>${marker}${seriesName}: ${formatValue(
                value
            )}`;
        } else {
            result = `<br/>${marker}${seriesName}: ${formatValue(value)}`;
        }
        return result;
    });
    return tooltipList.join('');
};

const labelFormatter = (params = {}) => {
    const {value = ''} = params;
    return `${parsePercent(value)}`;
};

const extendOptions = {
    chartId: 'RefundOnTeacherBarChart',
    tooltip: {
        trigger: 'axis',
        formatter: tooltipCallback
    },
    yAxis: {
        newYAxis: [
            {
                name: '',
                type: 'value',
                axisLabel: {formatter: value => numberHandle(value, 0)}
            },
            {
                name: '',
                type: 'value',
                axisLabel: {formatter: value => `${safeMultiple(value, 100)}%`}
            }
        ],
        common: {
            splitLine: {show: false}
        }
    },
    legend: {
        // orient: 'vertical',
        left: 'left'
    },
    grid: {
        left: 30,
        right: 50,
        bottom: 20,
        containLabel: true
    },
    series: {
        newYAxis: [
            {
                yAxisIndex: 0,
                name: '直播推题总数',
                // label: {
                //     show: false,
                //     formatter: ({value}) => numberHandle(value, 2),
                //     position: 'top'
                // }
            },
            {
                yAxisIndex: 1,
                name: '直播答题正确率',
                // label: {
                //     show: false,
                //     formatter: ({value}) => parsePercent(value),
                //     position: 'top'
                // }
            },
            {
                yAxisIndex: 1,
                name: '直播答题率',
                // label: {
                //     show: false,
                //     formatter: ({value}) => parsePercent(value),
                //     position: 'top'
                // }
            }
        ],
        common: {
            barMaxWidth: 30,
            // emphasis: {
            //     itemStyle: {
            //         shadowBlur: 10,
            //         shadowOffsetX: 0,
            //         shadowColor: 'rgba(0, 0, 0, 0.5)'
            //     }
            // }
        }
    }
};
const RefundOnTeacherBarChart = props => (
    <CommonEchart {...props} extendEchartsOptions={extendOptions} />
);

export default RefundOnTeacherBarChart;
